namespace OPC.Common
{
    using System;
    using System.Runtime.InteropServices;

    [ComVisible(true)]
    public class OpcServerList
    {
        private object EnumObj = null;
        private IEnumGUID ifEnum = null;
        private IOPCServerList ifList = null;
        private object OPCListObj = null;

        public void Dispose()
        {
            int num;
            this.ifEnum = null;
            if (this.EnumObj != null)
            {
                num = Marshal.ReleaseComObject(this.EnumObj);
                this.EnumObj = null;
            }
            this.ifList = null;
            if (this.OPCListObj != null)
            {
                num = Marshal.ReleaseComObject(this.OPCListObj);
                this.OPCListObj = null;
            }
        }

        ~OpcServerList()
        {
            this.Dispose();
        }

        public void ListAll(Guid catid, out OpcServers[] serverslist)
        {
            serverslist = null;
            this.Dispose();
            Guid clsid = new Guid("13486D51-4821-11D2-A494-3CB306C10000");
            Type typeFromCLSID = Type.GetTypeFromCLSID(clsid);
            this.OPCListObj = Activator.CreateInstance(typeFromCLSID);
            this.ifList = (IOPCServerList) this.OPCListObj;
            if (this.ifList == null)
            {
                Marshal.ThrowExceptionForHR(-2147467260);
            }
            this.ifList.EnumClassesOfCategories(1, ref catid, 0, ref catid, out this.EnumObj);
            if (this.EnumObj == null)
            {
                Marshal.ThrowExceptionForHR(-2147467260);
            }
            this.ifEnum = (IEnumGUID) this.EnumObj;
            if (this.ifEnum == null)
            {
                Marshal.ThrowExceptionForHR(-2147467260);
            }
            int celt = 300;
            IntPtr rgelt = Marshal.AllocCoTaskMem(celt * 0x10);
            int pceltFetched = 0;
            this.ifEnum.Next(celt, rgelt, out pceltFetched);
            if (pceltFetched < 1)
            {
                Marshal.FreeCoTaskMem(rgelt);
            }
            else
            {
                serverslist = new OpcServers[pceltFetched];
                byte[] destination = new byte[0x10];
                int num3 = (int) rgelt;
                for (int i = 0; i < pceltFetched; i++)
                {
                    serverslist[i] = new OpcServers();
                    Marshal.Copy((IntPtr) num3, destination, 0, 0x10);
                    serverslist[i].ClsID = new Guid(destination);
                    this.ifList.GetClassDetails(ref serverslist[i].ClsID, out serverslist[i].ProgID, out serverslist[i].ServerName);
                    num3 += 0x10;
                }
                Marshal.FreeCoTaskMem(rgelt);
                this.Dispose();
            }
        }

        public void ListAllData20(out OpcServers[] serverslist)
        {
            this.ListAll(new Guid("63D5F430-CFE4-11d1-B2C8-0060083BA1FB"), out serverslist);
        }
    }
}

